home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / hips / sources / tools / frame.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-10  |  3.5 KB  |  133 lines

  1. /*
  2. #    FRAMING . C
  3. %
  4. %    Copyright (c)    1990    Jin Guojun
  5. %
  6. %    framing -- put image into a frame
  7. %
  8. % AUTHOR:    Jin Guojun - LBL    11/15/90
  9. */
  10.  
  11. #include <math.h>
  12. #include "header.def"
  13. #include "imagedef.h"
  14.  
  15. arg_fmt_list_string    arg_fmt[] =    {
  16.     {"-D", "%d", True, 1, 0, "\t\tDebug level"},
  17.     {"-f", "%d %d", 0, 2, 1, "\trows [ columns ]    frame size\n\
  18. %    if no frame size given, framing will pick larger size of input    \n\
  19. %    rows and columns as frame size. If input rows equal to columns,    \n\
  20. %    framing will enlarge old frame size by 5%."},
  21.     {"-o", "%s", NULL, 1, 1, "output file name"},
  22.     {"-v", "%d", 255, 1, 0,
  23.     "frame value [%.0f if not # specified. default = 0]"},    NULL};
  24. int    fvalue;
  25. U_IMAGE    uimg;
  26.  
  27. #ifndef    FMODULE
  28. #define    FMODULE    16
  29. #endif
  30. #define    inbuf    uimg.src
  31. #define    obuf    uimg.dest
  32. #define    frm    uimg.frames
  33. #define    row    uimg.height
  34. #define    cln    uimg.width
  35. #define    pxl_bytes    uimg.pxl_out
  36.  
  37. #define    framing(f_type, in_buf, out_buf, i_r, i_c, o_r, o_c)    {    \
  38. register f_type    *ibuf=(f_type*)in_buf, *o_buf=(f_type*)out_buf;    \
  39.     int    ir = i_r, ic = i_c, or = o_r, oc = o_c;    \
  40.     int    mtop = (or - ir) >> 1,    \
  41.         mleft= (oc - ic) >> 1;    \
  42.     register int    r, c;        \
  43.         \
  44.     for (r=0; r < mtop; r++)    \
  45.         for (c=0; c < oc; c++)    \
  46.         *o_buf++ = fvalue;    \
  47.     for (r=0; r<ir; r++)    {    \
  48.         for (c=0; c < mleft; c++)    \
  49.             *o_buf++ = fvalue;    \
  50.         for (c=0; c < ic; c++)        \
  51.             *o_buf++ = *ibuf++;    \
  52.         for (c=0; c < oc-ic-mleft; c++)    \
  53.             *o_buf++ = fvalue;    \
  54.     }    \
  55.     for (r=0; r<or-ir-mtop; r++)    \
  56.         for (c=0; c<oc; c++)    \
  57.         *o_buf++ = fvalue;    \
  58. }
  59.  
  60.  
  61. main(argc, argv)
  62. int    argc;
  63. char**    argv;
  64. {
  65. char    **fl, *of_name;
  66. int    i_row, i_cln, o_row, o_cln=0, f, t2f;
  67. MType    fsize, ofsize;
  68.  
  69. format_init(&uimg, IMAGE_INIT_TYPE, HIPS, uimg.color_dpy=-1, *argv, "F10-3");
  70.  
  71.     if ((f=parse_argus(&fl, argc, argv, arg_fmt,
  72.         &debug, &o_row, &o_cln, &of_name, &fvalue)) < 0)
  73.         exit(f);
  74.     if (of_name && !(out_fp=freopen(of_name, "wb", stdout)))
  75. errout:        parse_usage(arg_fmt),    exit(0);
  76.     if (f && !(in_fp=freopen(fl[0], "rb", stdin)))
  77.         syserr("input file %s not found", fl[0]);
  78.  
  79. io_test(fileno(in_fp),    goto    errout);
  80.  
  81. (*uimg.header_handle)(HEADER_READ, &uimg, 0, 0);
  82. uimg.o_form = uimg.in_form;
  83. uimg.pxl_out = uimg.pxl_in;
  84. if (isColorImage(uimg.color_form) > 1)
  85.     uimg.color_form = CFM_ILC;
  86. t2f = uimg.color_form==CFM_ILC;
  87.  
  88. i_row = row;    i_cln = cln;
  89. fsize = i_row * i_cln;
  90. if (!o_row) {
  91.    if (row != cln)
  92.     o_row = o_cln = MAX(row, cln);
  93.    else    o_row = o_cln = row * 1.05;
  94. }
  95. else    {
  96.     if (o_row==row && o_cln==cln)
  97.         syserr("no thing done");
  98.     if (o_row < row)    o_row = row * 1.05;
  99.     if (!o_cln)    o_cln = o_row;
  100.     if (o_cln < cln)    o_cln = cln * 1.05;
  101. }
  102. o_cln += FMODULE - o_cln % FMODULE;
  103. row = o_row;    cln = o_cln;
  104.  
  105. (*uimg.header_handle)(HEADER_WRITE, &uimg, argc, argv, True);
  106.  
  107. cln = i_cln,    row = i_row;    /* restore for reading data */
  108. obuf = zalloc(t2f ? 4 : uimg.pxl_in, ofsize = o_row * o_cln, "obuf");
  109.  
  110. message("%s %d x %d x %d (x %d bytes)\n", *argv, o_row, o_cln, frm,
  111.             pxl_bytes = t2f ? 3 : uimg.pxl_in);
  112.  
  113. for (f=0; f<frm; f++)    {
  114.     (*uimg.std_swif)(FI_LOAD_FILE, &uimg, uimg.load_all=0, No);
  115.  
  116.     switch(pxl_bytes) {
  117.     case 1:    framing(byte, inbuf, obuf, i_row, i_cln, o_row, o_cln);
  118.         break;
  119.     case 2:    framing(short, inbuf, obuf, i_row, i_cln, o_row, o_cln);
  120.         break;
  121.     case 3:    of_name = nzalloc(4, fsize, "tbuf");
  122.         ilc_transfer(of_name, inbuf, fsize, 3, 0, 4);
  123.         free(inbuf);    inbuf = of_name;
  124.     case 4:    framing(long, inbuf, obuf, i_row, i_cln, o_row, o_cln);
  125.         if (t2f)        ilc_transfer(obuf, obuf, ofsize, 4, 0, 3);
  126.         break;
  127.     default:    syserr("wrong format");
  128.     }
  129.     if (fwrite(obuf, pxl_bytes, ofsize, stdout) != ofsize)
  130.         syserr("write new frame %d", f);
  131. }
  132. }
  133.